package com.xijia.wuyouwallet.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.xijia.wuyouwallet.R;
import com.xijia.wuyouwallet.api.MyCallBack;
import com.xijia.wuyouwallet.base.BaseActivity;
import com.xijia.wuyouwallet.utils.CharCheckUtil;
import com.xijia.wuyouwallet.utils.MyLogger;
import com.xijia.wuyouwallet.utils.UrlConfig;
import com.xijia.wuyouwallet.utils.XUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/14 0014.
 */

public class RegisterActivity extends BaseActivity implements View.OnClickListener{

    private TextView register_login,register_getCode,register_config;
    private EditText register_phone,register_code,register_recommend;
    private ImageView register_isAgree,register_back;

    private boolean isAgree = true;

    private int countSeconds = 60;// 倒数秒数

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
    }
    private Handler hanlder = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    if (countSeconds > 0) {
                        --countSeconds;
                        register_getCode.setText(countSeconds + "s");
                        hanlder.sendEmptyMessageDelayed(0, 1000);
                    } else {
                        countSeconds = 60;
                        register_getCode.setText("重新获取");
                        register_getCode.setClickable(true);
                    }
                    break;
            }
        }
    };
    // 开始倒计时
    private void startCountBack() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                register_getCode.setText(countSeconds + "s");
                hanlder.sendEmptyMessage(0);
                register_getCode.setClickable(false);
            }
        });
    }
    private void initView(){
        register_back = (ImageView) findViewById(R.id.register_back);
        register_back.setOnClickListener(this);
        register_login = (TextView) findViewById(R.id.register_login);
        register_login.setOnClickListener(this);
        register_getCode = (TextView) findViewById(R.id.register_getCode);
        register_getCode.setOnClickListener(this);
        register_config = (TextView) findViewById(R.id.register_config);
        register_config.setOnClickListener(this);
        register_phone = (EditText) findViewById(R.id.register_phone);
        register_code = (EditText) findViewById(R.id.register_code);
        register_recommend = (EditText) findViewById(R.id.register_recommend);
        register_isAgree = (ImageView) findViewById(R.id.register_isAgree);
        register_isAgree.setOnClickListener(this);
    }
    private void register()
    {
       String phone = register_phone.getText().toString().trim();
        if (phone.isEmpty()){
            showToast("请输入手机号");
            return;
        }
        if (!CharCheckUtil.isPhoneNum(phone)){
            showToast("手机号格式错误");
            return;
        }
        String code = register_code.getText().toString().trim();
        if (code.isEmpty()){
            showToast("请输入验证码");
            return;
        }
        if (!isAgree){
            showToast("请同意注册协议");
            return;
        }
        String rphone = register_recommend.getText().toString().trim();
        if (rphone.isEmpty()){
            showToast("请输入推荐人号码");
        }
        if (!CharCheckUtil.isPhoneNum(rphone)){
            showToast("手机号格式错误");
            return;
        }
        checkCode(code,phone,rphone);
    }
    private void checkCode(final String code, final String phone, final String rphone){
        Map<String,Object> map = new HashMap<>();
        map.put("phone",phone);
        map.put("code",code);
        showProgressDialog(true);
        XUtil.Post(UrlConfig.CHECKING_PHONECODE_URL,map,new MyCallBack<String>(){

            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                dismissProgressDialog();
                JSONObject jsonObject = JSONObject.parseObject(result);
                if (jsonObject.getInteger("code")==200){
                    Bundle bundle = new Bundle();
                    bundle.putString("phone",phone);
                    bundle.putString("rphone",rphone);
                    bundle.putString("code",code);
                    jumpToPage(RegisterTwoActivity.class,bundle,true);
                }else {
                    dismissProgressDialog();
                    showToast(jsonObject.getString("msg"));
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                dismissProgressDialog();
                MyLogger.wLog().e("验证短信onError="+ex.toString());
            }
        });
    }
    private void getCode(){
        String phone = register_phone.getText().toString().trim();
        if (phone.isEmpty()){
            showToast("请输入手机号");
            return;
        }
        if (!CharCheckUtil.isPhoneNum(phone)){
            showToast("手机号格式错误");
            return;
        }
        Map<String,Object> map = new HashMap<>();
        map.put("phone",phone);
        XUtil.Post(UrlConfig.GETCODE_URL,map,new MyCallBack<String>(){

            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                //MyLogger.wLog().e("获取验证码result="+result);
                JSONObject jsonObject = JSONObject.parseObject(result);
                if (jsonObject.getInteger("code")==200){
                    startCountBack();
                }
                showToast(jsonObject.getString("msg"));
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                MyLogger.wLog().e("获取验证码失败");
            }
        });
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.register_back:
                finish();
                break;
            case R.id.register_login:
                jumpToPage(LoginActivity.class);
                finish();
                break;
            case R.id.register_getCode:
                getCode();
                break;
            case R.id.register_config:
                register();
                break;
            case R.id.register_isAgree:
                if (isAgree){
                    register_isAgree.setImageResource(R.mipmap.login_agree_normal);
                    isAgree = false;
                }else {
                    register_isAgree.setImageResource(R.mipmap.login_agree_selected);
                    isAgree = true;
                }
                break;
        }
    }
}
